import { IBoot , Application } from "egg";
import { join } from 'path'
import mongoose from "mongoose";
export default class AppBoot implements IBoot {
    private readonly app: Application;
    constructor(app: Application) {
        this.app = app;
        //获取连接地址
        const { url } = app.config.mongoose;
        if(!url) {
            throw new Error('mongoose url is required')
        }
        //创建连接
        const db = mongoose.createConnection(url)
        //监听连接成功
        db.on('connected',()=>{
            app.logger.info('mongoose connected')
        })
        //绑定到app实例上
        app.mongoose = db
    }
    async willReady() {
        //计算文件路径
       const dir = join(this.app.baseDir, 'app/model')
       //将app/model下的文件加载到app上
       this.app.loader.loadToApp(dir, 'model', {
        //首字母大写
        caseStyle: 'upper',
       })
        
    }

}